define(function(require, exports, module){
	var EventProxy = require("eventproxy");
	var api = require("api");
	var data = require("data");
	var Loading = require("loading").Loading;

	function Bookmark(container) {
		this.container = $("#" + container);
		this.init();
	}

	Bookmark.prototype = $.extend(new EventProxy(), {
		init: function(){
			var _this = this;

			// 初始化Loading

			this.loading = new Loading({
				container: this.container
			});

			this.loading.start();

			var iconSuffixList = [
				"icon", "ico"
			];

			var iconMap = {};
			$.each(iconSuffixList, function(index, suffix){
				if (index < iconSuffixList.length) {
					iconMap[suffix] = iconSuffixList[index+1];
				} else {
					iconMap[suffix] = null;
				}
			});

			this.iconMap = iconMap;

			this.ul = $("<ul></ul>");
			this.container.empty();
			this.container.append(this.ul);

			this.ul.on("click", "a.action", function(){
				var bookmarkid = $(this).data("bookmarkid");
				var row = _this.ul.find("li[data-bookmarkid='"+bookmarkid+"']");
				row.hide();

				data.call("bookmark/remove", {id: bookmarkid}).done(function(json){
					row.remove();
				}).fail(function(){
					alert('删除分类失败');
					row.show();
				});
			});

			this.ul.on("dragstart", "li", function(e){
				var bookmarkid = $(this).data("bookmarkid");
				e.originalEvent.dataTransfer.setData("Text", bookmarkid);
			});
		},
		bookmarkMoved: function(bookmarkid){
			this.ul.find("li[data-bookmarkid='"+bookmarkid+"']").remove();
			if (this.ul.find("li[data-bookmarkid]").length == 0) {
				$("<li><p class='no-bookmarks'>此分类没有书签</p></li>").appendTo(this.ul);
			}
		},
		show: function(cateId, keyword){
			var _this = this;
			_this.ul.empty();
			this.cateId = cateId;

			// this.loading.start();
			
			data.call("bookmark/list", {
				cateId: cateId,
				keyword: keyword
			}).done(function(json){
				if (_this.cateId != cateId) {
					// 如果分类已经改变，就不要再加载数据进去了。
					return ;
				}

				_this.ul.empty();
				var bookmarks = json.data;

				if (bookmarks.length > 0) {
					bookmarks.forEach(function(bookmark, index){
						var indexOfSep = bookmark.url.indexOf("://");
						var indexOfFirstSlash = bookmark.url.indexOf("/", indexOfSep + 3);
						var rootPath = bookmark.url.substr(0, indexOfFirstSlash + 1);
						var icon = rootPath + "favicon.ico";
						$("<li data-bookmarkid='"+bookmark.id+"' draggable=\"true\"><a  class='link' href=\"" + bookmark.url + "\" target=\"_blank\"><img class=\"icon\" src=\"" + icon + "\" alt=\"\" width='16' height='16'/>"+bookmark.title+"</a><a class='action' href='javascript:;' data-bookmarkid='"+bookmark.id+"'><img src='images/icons/remove2.png' /></a></li>").appendTo(_this.ul);
					});

					_this.ul.find("img.icon").on("error", function(){
						var src = $(this).attr("src");
						var suffixPointIndex = src.lastIndexOf(".");
						var p1 = src.substring(0, suffixPointIndex);
						var suffix = src.substring(suffixPointIndex + 1);
						var nextSuffix = _this.iconMap[suffix];
						if (nextSuffix) {
							$(this).attr("src", p1 + "." + nextSuffix);
						} else {
							$(this).attr("src", "");
							$(this).off("error");
						}
					});					
				} else {
					$("<li><p class='no-bookmarks'>此分类没有书签</p></li>").appendTo(_this.ul);
				}

				_this.loading.stop();
			});
		},
		stopLoading: function(){
			this.loading.stop();
		}
	});

	return Bookmark;
});
